package com.Model.Pojo;

import com.Model.Date.DateTime;
import com.Model.GetSqlSession;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class LoginRecord { //用户的登录记录表类
    private int id; //唯一标识
    private String login_username; //用户名
    private String user_roles; //用户角色
    private String login_IP; //本次登录的IP
    private String user_status; //用户登录的状态
    private String login_time; //登录时间


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLogin_username() {
        return login_username;
    }

    public void setLogin_username(String login_username) {
        this.login_username = login_username;
    }

    public String getUser_roles() {
        return user_roles;
    }

    public void setUser_roles(String user_roles) {
        this.user_roles = user_roles;
    }

    public String getLogin_IP() {
        return login_IP;
    }

    public void setLogin_IP(String login_IP) {
        this.login_IP = login_IP;
    }

    public String getUser_status() {
        return user_status;
    }

    public void setUser_status(String user_status) {
        this.user_status = user_status;
    }

    public String getLogin_time() {
        return login_time;
    }

    public void setLogin_time(String login_time) {
        this.login_time = login_time;
    }

    public void setUserLoginRecord(String username, String roles) throws IOException {
        GetSqlSession getSqlSession = new GetSqlSession();//初始化获取sqlSession的对象
        SqlSession sqlSession = getSqlSession.getSqlSession();//获取sqlSession
        this.login_username = username;
        if(roles.equals("users")){
            this.user_roles = "普通用户";
        }else if (roles.equals("adm")){
            this.user_roles = "管理员";
        }else{
            this.user_roles = "游客";
        }
        this.user_status = getStatus(this.login_IP);
        DateTime dateTime = new DateTime();
        this.login_time = dateTime.getSettime();
        sqlSession.insert("addLoginRecord",this); //添加登录记录

        sqlSession.commit();
        sqlSession.close();//关闭连接
    }

    public ArrayList<LoginRecord> getUserLoginRecord(String name) throws IOException {
        ArrayList<LoginRecord> loginRecordslist = new ArrayList<LoginRecord>();
        LoginRecord loginRecord = null;
        GetSqlSession getSqlSession = new GetSqlSession();//初始化获取sqlSession的对象
        SqlSession sqlSession = getSqlSession.getSqlSession();//获取sqlSession
        Users users = sqlSession.selectOne("getUsersByNameOrShoujihaoOrYouxiang",loginRecord);//获取user的详细信息

        sqlSession.commit();
        sqlSession.close();//关闭连接
        return  loginRecordslist;
    }

    public String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP");
        if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
            return ip;
        }
        ip = request.getHeader("X-Forwarded-For");
        if (!StringUtils.isBlank(ip) && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个IP值，第一个为真实IP。
            int index = ip.indexOf(',');
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                return ip;
            }
        } else {
            return request.getRemoteAddr();
        }
    }

    private String getStatus(String login_ip) {
        String status;
        String ip = "上次ip";
        if(!login_ip.equals(ip)){ //如果登录的ip和上次的ip不一样，就判定登录状态为异常
            //获取上次ip还有问题，暂时这样处理
            status = "正常";
        }else {
            status = "异常";
        }
        return status;
    }
}
